package com.java.reggie.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.reggie.common.R;
import com.java.reggie.dto.SetmealDto;
import com.java.reggie.entity.Category;
import com.java.reggie.entity.Dish;
import com.java.reggie.entity.Setmeal;
import com.java.reggie.entity.SetmealDish;
import com.java.reggie.service.CategoryService;
import com.java.reggie.service.SetmealDishService;
import com.java.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        //分页构造器
        Page<Setmeal> pageInfo =  new Page<>(page,pageSize);
        Page<SetmealDto> setmealDto = new Page<>();//1、创建dto对象

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);

        //2、对象拷贝,records属性不用拷贝，里面存贮的是套餐的基本信息，当时是T类型，我们需要Setmeal，所以后面自己设置records
        BeanUtils.copyProperties(page,setmealDto,"records");
        List<Setmeal> records = pageInfo.getRecords();

        //3、
        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto setmealDto1 = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto1);
            //获取分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询套餐信息
            Category category = categoryService.getById(categoryId);
            if (category != null){
                //获取分类名称
                String categoryName = category.getName();
                setmealDto1.setCategoryName(categoryName);
            }
            return setmealDto1;
        }).collect(Collectors.toList());

        setmealDto.setRecords(list);
        return R.success(setmealDto);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 停售/起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        Setmeal setmeal = new Setmeal();
        for (Long id : ids) {
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("ok");
    }


    @GetMapping("/{id}")
    public R<SetmealDto> list(@PathVariable Long id){
        SetmealDto getWithDish = setmealService.getByIdWithDish(id);
        return R.success(getWithDish);
    }

    @GetMapping("/list")
    public R<List<Setmeal>> listFront(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐成功");
    }
}
